//vuex 状态管理

import Vue from "vue"
import Vuex from "vuex"

Vue.use(Vuex)

const store = new Vuex.Store({
	//数据源
	state:{
		//获取用户userId
		userId:uni.getStorageSync('Get_id') || '',
		
		// historyLists:[],
		 historyLists:uni.getStorageSync('__history') || [],
		 // author:'111'
		 
		 userinfo:uni.getStorageSync('USERINFO') || {},
		 
		 //收集formData信息
		 formData:uni.getStorageSync('FORMDATA') || {}
	},
	mutations:{
		SET_USER_INFO(state,userinfo){
			state.userinfo = userinfo
		},
		
		SET_HISTORY_LISTS(state,history){
			state.historyLists=history
		},
		CLEAR_HISTORY(state){
			state.historyLists = []
		},
		SET_AUTHOR(state,author){
			state.author=author
		},
	    //获取用户_id信息
		Get_id(state, payload){
			state.userId = payload
		},
		//收集formData信息
		FORMDATA(state,formData){
			state.formData = formData
		}
	},
	actions:{
		set_Get_id({commit},userId){
			uni.setStorageSync("Get_id",userId)
			commit('SET_USER_INFO',userId)
		},
		set_userinfo({commit},userinfo){
			uni.setStorageSync("USERINFO",userinfo)
			commit('SET_USER_INFO',userinfo)
		},
		set_history({commit,state},history){
			let list = state.historyLists
			list.unshift(history)
			uni.setStorageSync("__history",list)
			commit("SET_HISTORY_LISTS",list)
		},
		clearHistory({commit}){
			// uni.setStorageSync("__history",[])
			uni.removeStorageSync("__history")
			commit('CLEAR_HISTORY')
		},
		// setAuthor({commit,state},author){
		// 	commit("SET_AUTHOR",author)
		// }
		
		//收集formData信息
		setFormData({commit,state},formData){
			commit("FORMDATA",formData)
		}
	}
})
export default store